package com.macro.mall.portal.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.PageHelper;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.macro.mall.dto.*;
import com.macro.mall.mapper.BmsTourismProjectActivitiesApplicantMapper;
import com.macro.mall.model.BmsTourismProjectActivitiesApplicant;
import com.macro.mall.model.BmsTourismProjectActivitiesApplicantExample;
import com.macro.mall.model.UmsMember;
import com.macro.mall.portal.service.BmsTourismProjectActivitiesApplicantService;
import com.macro.mall.portal.service.BmsTourismProjectActivitiesService;
import com.macro.mall.portal.util.MemberUtil;
import com.macro.mall.portal.util.MemberUtils;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Base64;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class BmsTourismProjectActivitiesApplicantServiceImpl extends ActivitiesBaseServiceImpl implements BmsTourismProjectActivitiesApplicantService {

    @Autowired
    private BmsTourismProjectActivitiesApplicantMapper bmsTourismProjectActivitiesApplicantMapper;

    @Autowired
    private BmsTourismProjectActivitiesService bmsTourismProjectActivitiesService;

    @Override
    public List<BmsTourismProjectActivitiesApplicant> getList(BmsTourismProjectActivitiesApplicantParam bmsTourismProjectActivitiesApplicantParam) {
        if (Objects.isNull(bmsTourismProjectActivitiesApplicantParam.getTourismProjectActivitiesId())) {
            return Lists.newArrayList();
        }
        Integer pageNum = bmsTourismProjectActivitiesApplicantParam.getPageNum();
        Integer pageSize = bmsTourismProjectActivitiesApplicantParam.getPageSize();
        if (Objects.nonNull(pageNum) && Objects.nonNull(pageSize)) {
            PageHelper.startPage(pageNum, pageSize);
        }

        BmsTourismProjectActivitiesApplicantExample example = new BmsTourismProjectActivitiesApplicantExample();
        BmsTourismProjectActivitiesApplicantExample.Criteria criteria = example.createCriteria();
        criteria.andIsDelete(0);
        criteria.andTourismProjectActivitiesIdEqualTo(bmsTourismProjectActivitiesApplicantParam.getTourismProjectActivitiesId());
        if (StringUtils.isNotBlank(bmsTourismProjectActivitiesApplicantParam.getUserName())) {
            criteria.andUserNameLike("%" + bmsTourismProjectActivitiesApplicantParam.getUserName() + "%");
        }
        if (StringUtils.isNotBlank(bmsTourismProjectActivitiesApplicantParam.getUserPhone())) {
            criteria.andUserPhoneLike("%" + bmsTourismProjectActivitiesApplicantParam.getUserPhone() + "%");
        }
        if (Objects.nonNull(bmsTourismProjectActivitiesApplicantParam.getUserIdType())) {
            criteria.andUserIdTypeEqualTo(bmsTourismProjectActivitiesApplicantParam.getUserIdType());
        }
        if (StringUtils.isNotBlank(bmsTourismProjectActivitiesApplicantParam.getUserIdCard())) {
            criteria.andUserIdCardLike("%" + bmsTourismProjectActivitiesApplicantParam.getUserIdCard() + "%");
        }
        if (Objects.nonNull(bmsTourismProjectActivitiesApplicantParam.getApplicationTime())) {
            criteria.andApplicationTimeGreaterThanOrEqualTo(bmsTourismProjectActivitiesApplicantParam.getApplicationTime());
        }
        if (StringUtils.isNotBlank(bmsTourismProjectActivitiesApplicantParam.getStatus())) {
            criteria.andStatusEqualTo(bmsTourismProjectActivitiesApplicantParam.getStatus());
        }

        List<BmsTourismProjectActivitiesApplicant> list = bmsTourismProjectActivitiesApplicantMapper.selectByExample(example);
        return list;
    }

    @Override
    public BmsTourismProjectActivitiesApplicantDetail getItem(Long id) {
        BmsTourismProjectActivitiesApplicant bmsTourismProjectActivitiesApplicant = bmsTourismProjectActivitiesApplicantMapper.selectByPrimaryKey(id);
        BmsTourismProjectActivitiesApplicantDetail detail = new BmsTourismProjectActivitiesApplicantDetail();
        BeanUtils.copyProperties(bmsTourismProjectActivitiesApplicant, detail);
        return detail;
    }

    @Override
    public void handleRegistrationStatus(String tourismProjectActivitiesId, String status){
        if (StringUtils.isBlank(tourismProjectActivitiesId)) {
            throw new NullPointerException("tourismProjectActivitiesId is null");
        }
        UmsMember wxUser = MemberUtils.getUser();

        if (Objects.isNull(wxUser)) {
            throw new NullPointerException("wxUser is null");
        }
        BmsTourismProjectActivitiesApplicant projectActivitiesApplicant = new BmsTourismProjectActivitiesApplicant();
        projectActivitiesApplicant.setStatus(status);
        BmsTourismProjectActivitiesApplicantExample example = new BmsTourismProjectActivitiesApplicantExample();
        example.createCriteria().andTourismProjectActivitiesIdEqualTo(Long.valueOf(tourismProjectActivitiesId)).andUserIdEqualTo(wxUser.getId());
        bmsTourismProjectActivitiesApplicantMapper.updateByExampleSelective(projectActivitiesApplicant,example);
    }

    private void handleRegistrationStatus(String tourismProjectActivitiesId, String status, UmsMember wxUser){
        if (StringUtils.isBlank(tourismProjectActivitiesId)) {
            throw new NullPointerException("tourismProjectActivitiesId is null");
        }
        if (Objects.isNull(wxUser)) {
            throw new NullPointerException("wxUser is null");
        }
        BmsTourismProjectActivitiesApplicant projectActivitiesApplicant = new BmsTourismProjectActivitiesApplicant();
        projectActivitiesApplicant.setStatus(status);
        BmsTourismProjectActivitiesApplicantExample example = new BmsTourismProjectActivitiesApplicantExample();
        example.createCriteria().andTourismProjectActivitiesIdEqualTo(Long.valueOf(tourismProjectActivitiesId)).andUserIdEqualTo(wxUser.getId());
        bmsTourismProjectActivitiesApplicantMapper.updateByExampleSelective(projectActivitiesApplicant,example);
    }

    @Override
    Boolean checkBaseInfo(ActivitiesRegistrationParam registrationParam) {
        String activitiesId = registrationParam.getActivitiesId();
        UmsMember wxUser = registrationParam.getWxUser();
        BmsTourismProjectActivitiesApplicantExample example = new BmsTourismProjectActivitiesApplicantExample();
        example.createCriteria().andTourismProjectActivitiesIdEqualTo(Long.valueOf(activitiesId))
                .andUserIdEqualTo(wxUser.getId())
                .andStatusEqualTo("0").andIsDeleteEqualTo((byte) 0);
        List<BmsTourismProjectActivitiesApplicant> bmsTourismProjectActivitiesApplicants = bmsTourismProjectActivitiesApplicantMapper.selectByExample(example);
        return CollectionUtil.isEmpty(bmsTourismProjectActivitiesApplicants);
    }

    @Override
    int getActivitiesRegistratyCount(ActivitiesRegistrationParam registrationParam) {
        return 1;
    }

    @Override
    int getActivitiesHasRegistratyCount(ActivitiesRegistrationParam registrationParam) {
        return 1;
    }

    @Override
    ActivitiesRegistrationDetail getActivitiesDetail(String activitiesId) {
        BmsTourismProjectActivitiesDetail item = bmsTourismProjectActivitiesService.getItem(Long.valueOf(activitiesId));
        ActivitiesRegistrationDetail activitiesRegistrationDetail = new ActivitiesRegistrationDetail();
        activitiesRegistrationDetail.setApplicantCount(item.getPeopleNumber());
        activitiesRegistrationDetail.setCost(item.getCost());
        return activitiesRegistrationDetail;
    }

    @Override
    int insetApplicantList(ActivitiesRegistrationParam registrationParam) {
        BmsTourismProjectActivitiesDetail item = bmsTourismProjectActivitiesService.getItem(Long.valueOf(registrationParam.getActivitiesId()));
        UmsMember wxUser = registrationParam.getWxUser();
        BmsTourismProjectActivitiesApplicant projectActivitiesApplicant = new BmsTourismProjectActivitiesApplicant();
        projectActivitiesApplicant.setUserId(wxUser.getId());
        projectActivitiesApplicant.setUserName(wxUser.getUsername());
        projectActivitiesApplicant.setUserPhone(wxUser.getPhone());
        projectActivitiesApplicant.setApplicationTime(registrationParam.getApplicationTime());
        projectActivitiesApplicant.setTourismProjectActivitiesId(item.getId());
        projectActivitiesApplicant.setActualPayment(item.getCost());
        projectActivitiesApplicant.setShouldPayment(item.getCost());
        projectActivitiesApplicant.setStatus("0");
        projectActivitiesApplicant.setQrCode(null);
        projectActivitiesApplicant.setIsDelete((byte) 0);
        projectActivitiesApplicant.setCreateUser(wxUser.getId());
        projectActivitiesApplicant.setCreateTime(new Date());
        projectActivitiesApplicant.setUpdateUser(wxUser.getId());
        projectActivitiesApplicant.setUpdateTime(new Date());
        return bmsTourismProjectActivitiesApplicantMapper.insertSelective(projectActivitiesApplicant);
    }

    @Override
    int handleApplicantStatus(ActivitiesRegistrationParam registrationParam, String status) {
        String activitiesId = registrationParam.getActivitiesId();
        UmsMember wxUser = registrationParam.getWxUser();
        BmsTourismProjectActivitiesApplicantExample example = new BmsTourismProjectActivitiesApplicantExample();
        example.createCriteria().andTourismProjectActivitiesIdEqualTo(Long.valueOf(activitiesId))
                .andUserIdEqualTo(wxUser.getId())
                .andStatusEqualTo("0").andIsDeleteEqualTo((byte) 0);
        List<BmsTourismProjectActivitiesApplicant> bmsTourismProjectActivitiesApplicants = bmsTourismProjectActivitiesApplicantMapper.selectByExample(example);

        BmsTourismProjectActivitiesApplicant activitiesApplicant = new BmsTourismProjectActivitiesApplicant();
        activitiesApplicant.setStatus(status);

        BmsTourismProjectActivitiesApplicantExample updateExample = new BmsTourismProjectActivitiesApplicantExample();
        updateExample.createCriteria().andIdEqualTo(bmsTourismProjectActivitiesApplicants.get(0).getId());
        return bmsTourismProjectActivitiesApplicantMapper.updateByExampleSelective(activitiesApplicant, updateExample);
    }
}
